package com.situ.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.situ.web.dao.IBanJiCourseDao;
import com.situ.web.entity.BanJi;
import com.situ.web.util.JDBCUtil;
import com.situ.web.util.ModelConvertUtil;

import sun.awt.RepaintArea;

public class BanJiCourseDaoImpl implements IBanJiCourseDao {

	@Override
	public List<Map<String, Object>> findPageList() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet  = null;
		List<Map<String, Object>> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT b.id AS b_id,b.name AS b_name,"
					+ "c.name AS c_name,c.credit AS c_credit FROM banji AS b"
					+ "  INNER JOIN banji_course AS bc ON b.id=bc.banji_id"
					+ " INNER JOIN course AS c ON bc.course_id=c.id;";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			list = ModelConvertUtil.converToList(resultSet);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<BanJi> selectBanJi() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<BanJi> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,name from banji;";
			preparedStatement = connection.prepareStatement(sql);
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				BanJi banJi = new BanJi(id, name);
				list.add(banJi);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

}
